package com.mangowang.module.main.ui;
/**
 * 文件名 : AllocaPartnerActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/26 15:53
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Mystock;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.lib.common.util.ValidateUtils;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.AllocMachineAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by AndroidIntexh1 on 2018/12/27.
 */

public class AllocMachineActivity extends BaseActivity {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    private AllocMachineAdapter mAdapter;
    private List<Mystock.myMachineType.goodsInfo> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alloca_machine);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        backIv.setOnClickListener(view -> finish());
        tvTitle.setText(getIntent().getStringExtra("title"));
        initRecyclerView();
        initData();
    }

    private void initData() {
        Mystock.myMachineType.Builder builder = Mystock.myMachineType.newBuilder();
        // 获取回拨的人
        if (ValidateUtils.isValidate(getIntent().getStringExtra("member_id"))) {
            builder.setMemberId(getIntent().getStringExtra("member_id"));
            NetworkManager.INSTANCE.post(Apis.myMachineType, builder.build().toByteArray(), new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) throws InvalidProtocolBufferException {
                    datas = Mystock.myMachineType.parseFrom(response).getGoodsInfoListList();
                    mAdapter.addData(datas);
                }
                @Override
                public void onError(int errorCode, String errorMessage) {
                    showToast(errorMessage);
                }
            });
        } else {
            // 获取调拨的人
            NetworkManager.INSTANCE.post(Apis.myMachineType, new OnRequestCallBack() {
                @Override
                public void onOk(byte[] response) throws InvalidProtocolBufferException {
                    datas = Mystock.myMachineType.parseFrom(response).getGoodsInfoListList();
                    mAdapter.addData(datas);
                }
                @Override
                public void onError(int errorCode, String errorMessage) {
                    showToast(errorMessage);
                }
            });
        }
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new AllocMachineAdapter(mContext, datas));
//        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Mystock.myMachineType.goodsInfo bean = datas.get(position);
            if (TextUtils.isEmpty(bean.getMsg())) {
                Intent intent = new Intent();
                intent.putExtra("goods_id", bean.getGoodsCommonid());
                intent.putExtra("goods_name", bean.getGoodsName());
                intent.putExtra("goods_activation_num", bean.getGoodsActivationNum());
                intent.putExtra("goods_unactivation_num", bean.getGoodsUnactivationNum());
                setResult(300, intent);
                finish();
            }
        });
    }
}
